<?php

namespace app\web\controller;

use app\common\controller\Api;
use think\Db;
use Carbon\Carbon;
use app\api\validate\User as UserValidate;

/**
 * 人脸识别
 * @package app\index\controller
 */
class UserFace extends Auth
{
    public $noNeedLogin = ['*'];

    public function detect(){
        self::search('http://houtai11.kamiyunhui.com/image/001.jpg');
    }


    /**
     * 人脸识别并注册
     * @param $imageUrl
     * @return array
     */
    public static function search($imageUrl){
        //先搜索
        $searchUrl = 'https://open.ys7.com/api/lapp/intelligence/face/analysis/search';
        $searchData = [
            'accessToken'=>'at.06cw27gd32d31ooo6i9e0dda634wmsrz-709ivixthh-1ehr7jz-de1dajxij',
            'dataType'=>'0',
            'image'=>$imageUrl,
            'operation'=>'[{"setToken":"6c723c8ccddc46a498ee2631b2122198","threshold":80,"matchCount":1}]',
            'topNum'=>'1',
        ];
        $resultSearch = http_request($searchUrl,$searchData);
        if($resultSearch['code'] == '200' &&  !empty($resultSearch['data']['results'])){
            //有数据
            $faceToken = $resultSearch['data']['results']['faceToken'];
            $userFaceData = Db::name('qt_user_face')->where(['face.faceToken'=>$faceToken])->find();
            $cardData = [];
            if($userFaceData['viper_card_id'] == null){
                $msg = '已经识别过的用户';
            }else{
                $cardData = Db::name('qt_viper_card')->where(['id'=>$userFaceData['viper_card_id']])->find();
            }
            $resultData['data'] = $cardData;
            $resultData['code'] = '200';
            $resultData['msg'] = $msg;
        }else{
            $requestUrl = 'https://open.ys7.com/api/lapp/intelligence/face/analysis/detect';
            $requestData = [
                'accessToken'=>'at.06cw27gd32d31ooo6i9e0dda634wmsrz-709ivixthh-1ehr7jz-de1dajxij',
                'dataType'=>'0',
                'image'=>$imageUrl,
                'operation'=>"gender,age,glass,mask,beard,hat,expression",
            ];
            $result = http_request($requestUrl,$requestData);
            if($result['code'] == '200' && !empty($result['data']['faces'])){
                $faceData =  $result['data']['faces'];
                foreach ($faceData as $key=>$value){
                    $find = Db::name('qt_user_face')->where(['faceToken'=>$value['faceToken']])->find();
                    if(empty($find)) {
                        try{
                            //插入数据并注册
                            $insertData['image_url'] = $imageUrl;
                            $insertData['faceToken'] = $value['faceToken'];
                            $insertData['age'] = $value['age'] != null ? $value['age']['value'] : null;
                            $insertData['gender'] = $value['gender'] != null ? $value['gender']['value'] : null;
                            $insertData['glass'] = $value['glass'] != null ? $value['glass']['value'] : null;
                            $insertData['smile'] = $value['smile'] != null ? $value['smile']['value'] : null;
                            $insertData['mask'] = $value['mask'] != null ? $value['mask']['value'] : null;
                            $insertData['hat'] = $value['hat'] != null ? $value['hat']['value'] : null;
                            $insertData['beard'] = $value['beard'] != null ? $value['beard']['value'] : null;
                            $insertData['create_time'] = time();
                            Db::name('qt_user_face')->insert($insertData);
                            $registerUrl = 'https://open.ys7.com/api/lapp/intelligence/face/set/register';
                            $registerData = [
                                'accessToken'=>'at.06cw27gd32d31ooo6i9e0dda634wmsrz-709ivixthh-1ehr7jz-de1dajxij',
                                'faceTokens'=>$value['faceToken'],
                                'setToken'=>'6c723c8ccddc46a498ee2631b2122198',
                            ];
                            $registerResult = http_request($registerUrl,$registerData);
                            if($registerResult['code'] == '200'){
                                $resultData['data'] = [];
                                $resultData['code'] = '200';
                                $resultData['msg'] = '新客人脸注册成功';
                            }else{
                                $resultData['data'] = [];
                                $resultData['code'] = '200';
                                $resultData['msg'] = '新客人脸注册失败';
                            }
                        }catch (Exception $exception){
                            $resultData['data'] = [];
                            $resultData['code'] = '200';
                            $resultData['msg'] = '新客人脸注册失败';
                        }
                    }
                }
            }
        }
        return $resultData;
    }

}
